package com.ias.agdis_r.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.task.Task;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.util.S;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


@SuppressLint("UseSparseArrays")
public class NFTaskListViewAdapter extends BaseAdapter {

    private Context context = null;
    private LayoutInflater inflater = null;
    public Map<Integer, String> tnbMap = new HashMap<Integer, String>(); // 键为LIST的postion,值为任务的tnb
    private LinkedHashMap<String, Task> taskLinkedMap = null;
    List<Entry<String, Task>> taskList = null;
    public boolean isHistroy = false;
    private int SORT_TYPE = -1;                      //未选择排序方式

    public NFTaskListViewAdapter(Context context, boolean his, boolean forward) {
        this.context = context;
        this.isHistroy = his;
        initData();
        // 初始化TNB的MAP
        initTnbMap();
        sort();
    }

    private void initData() {
        if (isHistroy) {
            taskLinkedMap = DataManager.getInstance().getNFTaskHistroyWorkList();
        } else {
            taskLinkedMap = DataManager.getInstance().getWorkNFTaskList();
            // ReceiveTaskList与WorkTaskList合并显示
            taskLinkedMap.putAll(DataManager.getInstance().getReceiveNFTaskList());
        }
    }


    private void initTnbMap() {
        // TODO Auto-generated method stub
        tnbMap.clear();
        Set<String> tnbSet = taskLinkedMap.keySet();
        int i = 0;
        for (String tnb : tnbSet) {
            tnbMap.put(i, tnb);
            i++;
        }
    }

    public final class ViewHolder {
        public TextView txt_Name, txt_KaiShi, txt_JieShu,txt_PNM;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = new ViewHolder();

        if (convertView == null) {// 如果加载item布局为null
            inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.item_task_nflight_list, null);// 设置加载布局
            holder.txt_Name = (TextView) convertView
                    .findViewById(R.id.txt_Name);
            holder.txt_KaiShi = (TextView) convertView
                    .findViewById(R.id.txt_KaiShi);
            holder.txt_JieShu = (TextView) convertView
                    .findViewById(R.id.txt_JieShu);
            holder.txt_PNM = (TextView) convertView.findViewById(R.id.txt_PNM);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 加载数据
        Task task = taskLinkedMap.get(tnbMap.get(position));
        if (task != null) {
            holder.txt_Name
                    .setText(S.valueOf(task.getTaskInfo().getTNM()));
            String sStart = context.getString(R.string.start_time);
            String sEnd = context.getString(R.string.end_time);
            if(task.getServiceLabel("T_beginTime")!=null){

                sStart = sStart+ S.valueOf(task.getServiceLabel("T_beginTime").getCTNT());
            }
            if(task.getServiceLabel("T_endTime")!=null){
                sEnd = sEnd+ S.valueOf(task.getServiceLabel("T_endTime").getCTNT());
            }
            holder.txt_KaiShi.setText(sStart);
            holder.txt_JieShu.setText(sEnd);

            String text = S.valueOf(task.getTaskInfo().getSTN());
            if (S.isNotEmpty(text)) {
                if (text.equals(context.getString(R.string.paifa))) {
                    holder.txt_PNM.setText(context.getString(R.string.unaccept));
                    holder.txt_PNM.setBackgroundResource(R.drawable.textview_background2);
                } else if (MessageModel.T_ECXX.equals(task.getPID()) || MessageModel.T_ERJT.equals(task.getPID()) || MessageModel.T_EEND.equals(task.getPID())) {
                    holder.txt_PNM.setText(text);
                    holder.txt_PNM.setBackgroundResource(R.drawable.textview_background3);
                } else {
                    holder.txt_PNM.setText(text);
                    holder.txt_PNM.setBackgroundResource(R.drawable.textview_background);
                }
            } else {
                text = S.valueOf(task.getProcess(task.getPID()).getPCNM());
                if (text.equals(context.getString(R.string.paifa))) {
                    holder.txt_PNM.setText(context.getString(R.string.unaccept));
                    holder.txt_PNM.setBackgroundResource(R.drawable.textview_background2);
                } else if (MessageModel.T_ECXX.equals(task.getPID()) || MessageModel.T_ERJT.equals(task.getPID()) || MessageModel.T_EEND.equals(task.getPID())) {
                    holder.txt_PNM.setText(text);
                    holder.txt_PNM.setBackgroundResource(R.drawable.textview_background3);
                } else {
                    holder.txt_PNM.setText(text);
                    holder.txt_PNM.setBackgroundResource(R.drawable.textview_background);
                }
            }
        }
        return convertView;
    }

    public String getTNB(int position) {
        return tnbMap.get(position);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return taskLinkedMap.size();
    }

    @Override
    public Task getItem(int position) {
        // TODO Auto-generated method stub

        String tnb = tnbMap.get(position);
        Task task = taskLinkedMap.get(tnb);
        return task;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public void setSortType(int type) {
        this.SORT_TYPE = type;
    }

    public int getSortType() {
        return this.SORT_TYPE;
    }

    @Override
    public void notifyDataSetChanged() {
        initData();
        //排序
        sort();
        super.notifyDataSetChanged();
    }

    private void sort() {

        initTnbMap();

//        if (SORT_TYPE == -1) {
//            //初始化TNB的MAP
//            initTnbMap();
//            return;
//        }
//        taskList = new ArrayList<Entry<String, Task>>(taskLinkedMap.entrySet());
//        for (Entry<String, Task> entry : taskList) {
//            Log.i("任务列表-排序", "排序前：" + entry.getKey() + "|" + TaskUtil.getFlightLatestTime(S.valueOf(entry.getValue().getTaskInfo().getATO()), S.valueOf(entry.getValue().getTaskInfo().getETO()), S.valueOf(entry.getValue().getTaskInfo().getSTO())));
//        }
//
//        //通过比较器实现比较排序
//        Collections.sort(taskList, new Comparator<Entry<String, Task>>() {
//            public int compare(Entry<String, Task> o1, Entry<String, Task> o2) {
//
//                Task task1 = o1.getValue();
//                Task task2 = o2.getValue();
//                String com1 = "";
//                String com2 = "";
//                switch (SORT_TYPE) {
//
//                    case SORT_TIME_ASC:     //时间升序
//                        com1 = TaskUtil.getFlightLatestTime(S.valueOf(task1.getTaskInfo().getATO()), S.valueOf(task1.getTaskInfo().getETO()), S.valueOf(task1.getTaskInfo().getSTO()));
//                        com2 = TaskUtil.getFlightLatestTime(S.valueOf(task2.getTaskInfo().getATO()), S.valueOf(task2.getTaskInfo().getETO()), S.valueOf(task2.getTaskInfo().getSTO()));
//                        break;
//                    case SORT_TIME_DESC:     //时间降序
//                        com1 = TaskUtil.getFlightLatestTime(S.valueOf(task2.getTaskInfo().getATO()), S.valueOf(task2.getTaskInfo().getETO()), S.valueOf(task2.getTaskInfo().getSTO()));
//                        com2 = TaskUtil.getFlightLatestTime(S.valueOf(task1.getTaskInfo().getATO()), S.valueOf(task1.getTaskInfo().getETO()), S.valueOf(task1.getTaskInfo().getSTO()));
//                        break;
//                    case SORT_FLN_ASC:         //航班号升序
//                        com1 = S.valueOf(task1.getTaskInfo().getFLN());
//                        com2 = S.valueOf(task2.getTaskInfo().getFLN());
//                        break;
//                    case SORT_FLN_DESC:     //航班号降序
//                        com1 = S.valueOf(task2.getTaskInfo().getFLN());
//                        com2 = S.valueOf(task1.getTaskInfo().getFLN());
//                        break;
//                    case SORT_PSN_ASC:         //机位升序
//                        com1 = S.valueOf(task1.getTaskInfo().getPSN());
//                        com2 = S.valueOf(task2.getTaskInfo().getPSN());
//                        break;
//                    case SORT_PSN_DESC:         //机位降序
//                        com1 = S.valueOf(task2.getTaskInfo().getPSN());
//                        com2 = S.valueOf(task1.getTaskInfo().getPSN());
//                        break;
//                    case SORT_STATE_ASC:      //任务状态升序
//                        com1 = S.valueOf(task1.getProcess(task1.getPID()).getPIDX());
//                        com2 = S.valueOf(task2.getProcess(task2.getPID()).getPIDX());
//                        break;
//                    case SORT_STATE_DESC:      //任务状态降序
//                        com1 = S.valueOf(task2.getProcess(task2.getPID()).getPIDX());
//                        com2 = S.valueOf(task1.getProcess(task1.getPID()).getPIDX());
//                        break;
//                    case SORT_REG_ASC:          //机号升序
//                        com1 = S.valueOf(task1.getTaskInfo().getREG());
//                        com2 = S.valueOf(task2.getTaskInfo().getREG());
//                        break;
//                    case SORT_REG_DESC:      //机号降序
//                        com1 = S.valueOf(task2.getTaskInfo().getREG());
//                        com2 = S.valueOf(task1.getTaskInfo().getREG());
//                        break;
//                    case SORT_ACN_ASC:          //机型升序
//                        com1 = S.valueOf(task1.getTaskInfo().getACN());
//                        com2 = S.valueOf(task2.getTaskInfo().getACN());
//                        break;
//                    case SORT_ACN_DESC:       //机型降序
//                        com1 = S.valueOf(task2.getTaskInfo().getACN());
//                        com2 = S.valueOf(task1.getTaskInfo().getACN());
//                        break;
//                    case SORT_TYPE_ASC:       //任务类型升序
//                        com1 = S.valueOf(task1.getTaskInfo().getRNM());
//                        com2 = S.valueOf(task2.getTaskInfo().getRNM());
//                        break;
//                    case SORT_TYPE_DESC:       //任务类型降序
//                        com1 = S.valueOf(task2.getTaskInfo().getRNM());
//                        com2 = S.valueOf(task1.getTaskInfo().getRNM());
//                        break;
//                }
//
//                return com1.compareTo(com2);
//            }
//        });
//        tnbMap.clear();
//        int i = 0;
//        for (Entry<String, Task> entry : taskList) {
//            String tnb = entry.getValue().getTNB();
//            tnbMap.put(i, tnb);
//            i++;
//            Log.i("任务列表-排序", "排序后：" + entry.getKey() + "|" + TaskUtil.getFlightLatestTime(S.valueOf(entry.getValue().getTaskInfo().getATO()), S.valueOf(entry.getValue().getTaskInfo().getETO()), S.valueOf(entry.getValue().getTaskInfo().getSTO())));
//        }
    }
}
